/*
 * PhoneNumber.java
 */

package model;

/**
 * PhoneNumber class represents a ten digit US phone number.
 * 
 * @author Sean Brown
 * @author Carlo Consolacion
 * @version 2.0
 */
public class PhoneNumber implements Cloneable {

	/**
	 * The area code of this phone number (3 digit).
	 */
	private final int my_area_code;

	/**
	 * The first three digits of this phone number.
	 */
	private final int my_prefix;

	/**
	 * The last four digits of this phone number.
	 */
	private final int my_postfix;

	/**
	 * Constructor takes a String and determines if it is a valid phone number
	 * (setting its values to all zeros if it is invalid).
	 * 
	 * @param the_number
	 *            String representation of a Phone Number.
	 */
	public PhoneNumber(String the_number) {
		if (!validString(the_number)) {
			the_number = "5555555555";
		}
		my_area_code = Integer.parseInt(the_number.substring(0, 3));
		my_prefix = Integer.parseInt(the_number.substring(3, 6));
		my_postfix = Integer.parseInt(the_number.substring(6, 10));	
	}

	/**
	 * Returns whether or not the String is a valid US phone number.
	 * 
	 * @param the_number
	 *            The String to evaluate.
	 * @return True if valid, false if not.
	 */
	private boolean validString(final String the_number) {
		boolean valid = true;
		if (the_number.length() != 10) {
			valid = false;
		}
		for (char c : the_number.toCharArray()) {
			int i = Character.getNumericValue(c);
			if (i < 0 || i > 9) {
				valid = false;
			}
		}
		return valid;
	}

	/**
	 * Returns a String representation of the digits in this phone number.
	 * 
	 * @return String the numbers only in this phone number.
	 */
	public String getPhoneNumber() {
		return "" + String.format("%03d", my_area_code)
				+ String.format("%03d", my_prefix)
				+ String.format("%04d", my_postfix);
	}

	/**
	 * Overridden toString method returns a nicely formatted representation of
	 * this Phone Number "(###) ###-####"
	 * 
	 * @return String the formatted phone number.
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("(");
		sb.append(my_area_code);
		sb.append(")");
		sb.append(" ");
		sb.append(my_prefix);
		sb.append("-");
		sb.append(my_postfix);
		return sb.toString();
	}

	/**
	 * This method overrides the clone method.
	 * 
	 * @return An immutable copy of this Phone Number.
	 */
	@Override
	public Object clone() {
		return new PhoneNumber("" + getPhoneNumber());
	}

	/**
	 * This method overrides the equals method.
	 */
	@Override
	public boolean equals(final Object obj) {
		boolean equal = false;
		if (obj != null && getClass() == obj.getClass()) {
			equal = ((PhoneNumber) obj).hashCode() == hashCode();
		}
		return equal;
	}

	/**
	 * This method overrides the hashCode method.
	 */
	@Override
	public int hashCode() {
		return (my_area_code * 123) + (my_prefix * 456) + (my_postfix * 789);
	}

}
